package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.ProductFactory;
import com.clkj.xiuzheng.service.shangcheng.ScFactoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/Scfactory")
@CrossOrigin
@Api(value = "/Scfactory", tags = {"Z商城厂商管理"})
public class ScFactoryController {

    @Autowired
    private ScFactoryService scFactoryService;

    @ApiOperation(value = "获取厂商")
    @PostMapping("/getFactory")
    public Result getFactory(@ApiParam(name = "厂商名字", value = "factoryName")@RequestParam(required = false) String factoryName, @ApiParam(name = "公司id", value = "CompanyId")@RequestParam(required = false) Integer CompanyId) {
        try {
             List<ProductFactory> factoryList = scFactoryService.getFactory(factoryName,CompanyId);
            return new Result(true, StatusCode.OK,"查询成功",factoryList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "新增厂商")
    @PostMapping("/addFactory")
    public Result addFactory(@RequestBody ProductFactory productFactory) {
        try {
            scFactoryService.addFactory(productFactory);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation(value = "修改厂商状态")
    @PostMapping("/updateFactoryStatus")
    public Result updateFactory(@ApiParam(name = "Ids", value = "厂商id集合字符串用,分割") @RequestParam("Ids") String Ids,
                                @ApiParam(name = "status", value = "0下架 1上架") @RequestParam("status") Integer status) {
        try {
            scFactoryService.updateFactoryStatus(Ids,status);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
    @ApiOperation(value = "修改厂商名称")
    @PostMapping("/updateFactoryName")
    public Result updateFactoryName(@ApiParam(name = "factoryName", value = "厂商名称") @RequestParam("factoryName") String factoryName,
                                @ApiParam(name = "factoryId", value = "厂商id") @RequestParam("factoryId") Integer factoryId) {
        try {
            scFactoryService.updateFactoryName(factoryName,factoryId);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }

}
